package com.hunktimes.wholesale.ui.pages

import android.os.Bundle
import android.view.View
import android.widget.ImageView
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.hunktimes.wholesale.R
import com.hunktimes.wholesale.base.BaseFragment
import com.hunktimes.wholesale.databinding.FragmentOrderListV2Binding
import com.hunktimes.wholesale.databinding.ItemOrderBinding
import com.hunktimes.wholesale.extension.setImageUrl
import com.hunktimes.wholesale.mock.goods
import com.hunktimes.wholesale.util.Constants
import com.hunktimes.wholesale.util.L
import com.hunktimes.wholesale.widget.SmartRefreshDelegate

/**
 * Author: Delete
 * Date: 2020/7/27 20:07
 * Description:
 *
 */
class OrderListV2Fragment : BaseFragment(R.layout.fragment_order_list_v2), SmartRefreshDelegate {

    companion object {
        fun newInstance(args: Bundle? = null): OrderListV2Fragment {
            val fragment = OrderListV2Fragment()
            if (null != args) {
                fragment.arguments = args
            }
            return fragment
        }
    }

    lateinit var viewBinding: FragmentOrderListV2Binding
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewBinding = FragmentOrderListV2Binding.bind(view)
    }

    override fun onStart() {
        super.onStart()
        initOrders()
    }

    val orderListAdapter = OrderListV2Adapter()
    fun initOrders() {
        val ordersLm = LinearLayoutManager(requireContext())
        ordersLm.orientation = LinearLayoutManager.VERTICAL
        viewBinding.recyclerList.recyclerView.layoutManager = ordersLm
        viewBinding.recyclerList.recyclerView.adapter = orderListAdapter

        val data = mutableListOf<String>()
        for (i in 1..3) {
            data.add("購物車推薦$i")
        }
        orderListAdapter.data = data
        orderListAdapter.setOnItemClickListener { adapter, view, position ->
            rootNavController.navigate(R.id.navigation_order_detail)
        }
    }

    override fun onLoadMore() {
        L.d("OrderListV2Fragment position: ${arguments?.get(Constants.key_order_tab_type)} onLoadMore")
    }

    override fun onRefresh() {
        L.d("OrderListV2Fragment position: ${arguments?.get(Constants.key_order_tab_type)} onRefresh")
    }
}

class OrderListV2Adapter : BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_order) {
    override fun convert(holder: BaseViewHolder, item: String) {
        val orderItemViewBinding = ItemOrderBinding.bind(holder.itemView)
        val orderItemGoodsAdapter = OrderItemGoodsV2Adapter()
        val orderItemGoodsLm = LinearLayoutManager(context)
        orderItemGoodsLm.orientation = LinearLayoutManager.VERTICAL
        orderItemViewBinding.orderGoodsRcv.layoutManager = orderItemGoodsLm
        orderItemViewBinding.orderGoodsRcv.adapter = orderItemGoodsAdapter
        orderItemGoodsAdapter.setOnItemClickListener { _, _, _ ->
            getOnItemClickListener()?.onItemClick(this, holder.itemView, holder.adapterPosition)
        }

        val data = mutableListOf<String>()
//        data.add("Order Goods 1")
//        data.add("Order Goods 2")
        repeat(2) {
            data.add(goods.random())
        }
        orderItemGoodsAdapter.data = data
    }
}

class OrderItemGoodsV2Adapter :
    BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_order_goods) {
    override fun convert(holder: BaseViewHolder, item: String) {
        val coverIv = holder.getView<ImageView>(R.id.goods_cover_iv)
        coverIv.setImageUrl(item)
    }

}